let threeCount = 0
const timer = (time, res) => {
  return new Promise((resolve, reject) => {
    if (threeCount === 1) {
      setTimeout(() => {
        resolve(res)
      }, 2000);
    }
    setTimeout(() => {
      resolve(res)
    }, time);
    setTimeout(() => {
      if (time === 4000) {
        threeCount++
      }
      reject(res + '失败')
    }, 3000);
  })
}
let workQueue = []
let work1 = () => timer(2000, '第一个任务')
let work2 = () => timer(2000, '第二个任务')
let work3 = () => timer(4000, '第三个任务')
let work4 = () => timer(2000, '第四个任务')
let work5 = () => timer(2000, '第五个任务')
let work6 = () => timer(2000, '第六个任务')
let work7 = () => timer(2000, '第七个任务')
let work8 = () => timer(2000, '第八个任务')
let work9 = () => timer(2000, '第九个任务')
let work10 = () => timer(2000, '第十个任务')

workQueue.push(work1, work2, work3, work4, work5, work6, work7, work8, work9, work10)
let index = 1
let count = 0
function lunxun () {
  if (index === workQueue.length + 1) {
    return
  }
  let p = workQueue[index - 1]
  p().then((res) => {
    console.log(res);
    index++
    lunxun()
  }, (error) => {
    console.log(error);
    count++
    if (count >= 3) {
      return
    } else {
      lunxun()
    }
  })
}
lunxun()